<template>
	<view class="flex flex-direction margin-xs">
		<view class="uni-pagination-box">
			<uni-pagination :page-size="pageSize" :current="pageNo" :total="rowCount" @change="changePage" />
		</view>
		<view class="page-container">
			<buk-hotel-card v-for="(item, index) in hotels" :key="item.id" :hotel="item"></buk-hotel-card>
		</view>
		<view class="uni-pagination-box">
			<uni-pagination :page-size="pageSize" :current="pageNo" :total="rowCount" @change="changePage" />
		</view>
	</view>
</template>view>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useHotelStore
	} from '@/store/hotel.js'
	import {
		searchHotels
	} from '@/common/api/hotel.js'

	const hotelStore = useHotelStore()

	let hotels = ref([]);
	let loading = ref(false);
	let rowCount = ref(0)
	let pageNo = ref(1)
	let pageSize = ref(10)
	let pageTotal = ref(0)


	onLoad(() => {
		if (hotelStore.cityId == 0) {
			hotelStore.setSearchParams({
				"cityId": 2,
				cityName: "上海",
				checkInDate: "2025-04-01",
				checkOutDate: "2025-04-02",
				hotelName: "",
				roomCount: 1
			})
		}

		searchHotels2();
	})


	function searchHotels2() {
		// console.log('searchHotels2 begin: ....')
		// 调用查询接口
		// loading.value = true
		const params = {
			"sc.cityId": hotelStore.cityId,
			"sc.cityName": hotelStore.cityName,
			"sc.businessDistrict": "",
			"sc.checkInDate": hotelStore.checkInDate,
			"sc.checkOutDate": hotelStore.checkOutDate,
			"sc.hotelName": "",
			"sc.pageNo": pageNo.value,
			"sc.pageSize": pageSize.value,
			// "sc.time": "1741934058049"
		};

		// console.log(params)
		try {
			loading.value = true
			uni.showLoading({
				title: "查询中......"
			})
			searchHotels(
				params,
				(v) => {
					// console.log(v)
					hotels.value = v.hotels;
					pageNo.value = v.pageNo
					pageTotal.value = v.pageTotal
					rowCount.value = v.rowCount
					// console.log(hotels)
					// console.log(hotels.value)
				},
				() => {
					loading.value = false
					uni.hideLoading()
				}
			);
		} catch (err) {
			console.log(err)
		}
	}

	function viewHotelDetails(hotelInfo) {
		// console.log(type)
		// console.log(hotelInfo)
		hotelStore.updateHotelInfo(hotelInfo)
		uni.navigateTo({
			url: `/pages/hotel/HotelDetails`
		});
	}

	function changePage(e) {
		pageNo.value = e.current
		searchHotels2()
	}
</script>

<style>
	.right-align {
		display: flex;
		justify-content: flex-end;
		/* 水平靠右 */
		align-items: center;
		/* 垂直居中（可选） */
	}
</style>